package main

import (
	"image/color"
	"strconv"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"github.com/zhaocy/fyne-theme"
)

func main() {
	myApp := app.New()
	myApp.Settings().SetTheme(themex.Themex())
	myWin := myApp.NewWindow("按钮")

	labl := widget.NewLabel("")

	myBtn := NewButton("自定义Btn", func() {
		labl.SetText(myWin.Clipboard().Content())
	})
	myBtn.SetBgColor(color.RGBA{255, 0, 0, 100})
	myBtn.SetTextColor(color.RGBA{0, 0, 255, 100})
	myBtn.SetSize(fyne.NewSize(myBtn.Label.Size().Width+20, myBtn.Label.Size().Height+20))

	myWin.SetContent(container.NewVBox(labl, myBtn))
	myWin.Resize(fyne.NewSize(550, 450))
	myWin.ShowAndRun()
}

//自定义button
type Button struct {
	widget.DisableableWidget
	Text string

	Label      *canvas.Text
	background *canvas.Rectangle
	// tapBG      *canvas.Rectangle

	OnTapped func()
}

func NewButton(label string, tapped func()) *Button {
	lbl := canvas.NewText(label, color.Black)
	bg := canvas.NewRectangle(color.Black)
	button := &Button{
		Text:       label,
		Label:      lbl,
		background: bg,

		OnTapped: tapped,
	}
	button.ExtendBaseWidget(button)
	return button
}

func NewBootstrapButton(label, class string, tapped func()) *Button {
	btn := NewButton(label, tapped)
	switch class {
	case "primary":
		btn.SetBgColor(hex2rgb("0d6efd"))
	case "success":
		btn.SetBgColor(hex2rgb("198754"))
	default:
		return btn
	}
	return btn
}

func hex2rgb(str string) color.Color {
	if len(str) != 6 {
		return color.RGBA{255, 255, 255, 255}
	}
	r, _ := strconv.ParseInt(str[:2], 16, 10)
	g, _ := strconv.ParseInt(str[2:4], 16, 18)
	b, _ := strconv.ParseInt(str[4:], 16, 10)
	return color.RGBA{uint8(r), uint8(g), uint8(b), 255}
}

func (b *Button) SetText(text string) {
	b.Text = text

	b.Refresh()
}

func (b *Button) Tapped(*fyne.PointEvent) {
	if b.Disabled() {
		return
	}
	// b.Refresh()

	if b.OnTapped != nil {
		b.OnTapped()
	}
}

func (b *Button) MinSize() fyne.Size {
	b.ExtendBaseWidget(b)
	return fyne.NewSize(20, 10)
}

func (b *Button) SetBgColor(c color.Color) {
	b.background.FillColor = c
	b.Refresh()
}

func (b *Button) SetTextColor(c color.Color) {
	b.Label.Color = c
	b.Refresh()
}

func (b *Button) SetSize(size fyne.Size) {
	b.background.SetMinSize(size)
	b.Refresh()
}

func (b *Button) CreateRenderer() fyne.WidgetRenderer {
	b.ExtendBaseWidget(b)

	// text := canvas.NewText(b.Text, theme.ForegroundColor())
	text := b.Label

	// background := canvas.NewRectangle(theme.ButtonColor())
	background := b.background
	tapBG := canvas.NewRectangle(color.Transparent)
	objects := []fyne.CanvasObject{
		background,
		tapBG,
		text,
	}

	r := &buttonRenderer{
		objects: objects,
		button:  b,
		layout:  layout.NewCenterLayout(),
	}
	return r
}

type buttonRenderer struct {
	button  *Button
	layout  fyne.Layout
	objects []fyne.CanvasObject
}

func (r *buttonRenderer) Destroy() {

}
func (r *buttonRenderer) Objects() []fyne.CanvasObject {
	return r.objects
}
func (r *buttonRenderer) Layout(size fyne.Size) {
	// r.button.SetSize(r.layout.MinSize(r.objects))
	r.layout.Layout(r.objects, size)
}

func (r *buttonRenderer) MinSize() (size fyne.Size) {

	return fyne.NewSize(40, 10)
}

func (r *buttonRenderer) Refresh() {
	r.Layout(r.button.Size())

}
